from datetime import datetime
from app import db

class Banner(db.Model):
    """轮播图表"""
    __tablename__ = 'banners'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), comment='标题')
    image_url = db.Column(db.String(255), nullable=False, comment='图片地址')
    link_url = db.Column(db.String(255), comment='跳转链接')
    type = db.Column(db.Integer, default=1, comment='类型：1首页 2分类页')
    sort_order = db.Column(db.Integer, default=0, comment='排序')
    status = db.Column(db.Boolean, default=True, comment='状态 True启用 False禁用')
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='创建时间')

    def to_dict(self):
        return {
            'id': self.id,
            'title': self.title,
            'image': self.image_url,
            'link': self.link_url,
            'type': self.type,
            'sort': self.sort_order,
            'status': self.status,
            'create_datetime': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None
        }

    def __repr__(self):
        return f'<Banner {self.title}>' 